﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using QLKS2014.PUBLIC;
using System.Windows.Forms;

namespace QLKS2014.DATA
{
    public class PhongData
    {
        DataConnecter connect;
        public PhongData()
        {
            connect = new DataConnecter();
        }

        public List<PhongPublic> selectAll()
        {
            try
            {
                DataTable dt = new DataTable();
                List<PhongPublic> list = new List<PhongPublic>();

                int row;
                dt = connect.getData("Phong_SelectAll");
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    PhongPublic phongPublic = new PhongPublic();
                    phongPublic.MaPhong = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    phongPublic.TenPhong = dt.Rows[i].ItemArray[1].ToString();
                    phongPublic.TenLoaiPhong = dt.Rows[i].ItemArray[2].ToString();
                    phongPublic.DonGia = int.Parse(dt.Rows[i].ItemArray[3].ToString());
                    phongPublic.GhiChu = dt.Rows[i].ItemArray[4].ToString();
                    phongPublic.TinhTrang = dt.Rows[i].ItemArray[5].ToString();
                    list.Add(phongPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }

        public List<PhongPublic> selectByTenPhong(PhongPublic phong)
        {
            try
            {
                DataTable dt = new DataTable();
                List<PhongPublic> list = new List<PhongPublic>();
                int param = 1;
                string[] name = new string[param];
                object[] values = new object[param];

                name[0] = "@TenPhong";
                values[0] = phong.TenPhong;

                int row;
                dt = connect.getData("Phong_SelectByTenPhong", name, values, param);
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    PhongPublic phongPublic = new PhongPublic();

                    phongPublic.MaPhong = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    phongPublic.TenPhong = dt.Rows[i].ItemArray[1].ToString();
                    phongPublic.MaLoaiPhong = int.Parse(dt.Rows[i].ItemArray[2].ToString());
                    phongPublic.TenLoaiPhong = dt.Rows[i].ItemArray[3].ToString();
                    phongPublic.DonGia = int.Parse(dt.Rows[i].ItemArray[4].ToString());
                    phongPublic.GhiChu = dt.Rows[i].ItemArray[5].ToString();
                    phongPublic.TinhTrang = dt.Rows[i].ItemArray[6].ToString();
                    list.Add(phongPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }

        public List<PhongPublic> selectByMaLoaiPhong(PhongPublic phong)
        {
            try
            {
                DataTable dt = new DataTable();
                List<PhongPublic> list = new List<PhongPublic>();

                int param = 1;
                string[] name = new string[param];
                object[] values = new object[param];

                name[0] = "@MaLoaiPhong";

                values[0] = phong.MaLoaiPhong;

                int row;
                dt = connect.getData("Phong_SelectByMaLoaiPhong", name, values, param);
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    PhongPublic phongPublic = new PhongPublic();

                    phongPublic.MaPhong = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    phongPublic.TenPhong = dt.Rows[i].ItemArray[1].ToString();
                    phongPublic.MaLoaiPhong = int.Parse(dt.Rows[i].ItemArray[2].ToString());
                    phongPublic.TenLoaiPhong = dt.Rows[i].ItemArray[3].ToString();
                    phongPublic.DonGia = int.Parse(dt.Rows[i].ItemArray[4].ToString());
                    phongPublic.GhiChu = dt.Rows[i].ItemArray[5].ToString();
                    phongPublic.TinhTrang = dt.Rows[i].ItemArray[6].ToString();
                    list.Add(phongPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }

        public List<PhongPublic> selectPhongNotEmpty()
        {
            try
            {
                DataTable dt = new DataTable();
                List<PhongPublic> list = new List<PhongPublic>();


                int row;
                dt = connect.getData("Phong_SelectPhongNotEmpty");
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    PhongPublic phongPublic = new PhongPublic();
                    phongPublic.MaPhong = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    phongPublic.TenPhong = dt.Rows[i].ItemArray[1].ToString();
                    phongPublic.MaLoaiPhong = int.Parse(dt.Rows[i].ItemArray[2].ToString());
                    phongPublic.TenLoaiPhong = dt.Rows[i].ItemArray[3].ToString();
                    phongPublic.DonGia = int.Parse(dt.Rows[i].ItemArray[4].ToString());
                    phongPublic.GhiChu = dt.Rows[i].ItemArray[5].ToString();
                    phongPublic.TinhTrang = dt.Rows[i].ItemArray[6].ToString();
                    list.Add(phongPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }

        //public List<PhongPublic> selectByRentTime(DateTime ngayDen, DateTime ngayDi)
        //{
        //    try
        //    {
        //        DataTable dt = new DataTable();
        //        List<PhongPublic> list = new List<PhongPublic>();

        //        int param = 2;
        //        string[] name = new string[param];
        //        object[] values = new object[param];

        //        name[0] = "@NgayThueDau";
        //        name[1] = "@NgayThueCuoi";
        //        values[0] = ngayDen;
        //        values[1] = ngayDi;

        //        int row;
        //        dt = connect.getData("Phong_SelectByRentTime", name, values, param);
        //        row = dt.Rows.Count;
        //        for (int i = 0; i < row; i++)
        //        {
        //            PhongPublic phongPublic = new PhongPublic();

        //            phongPublic.MaPhong = int.Parse(dt.Rows[i].ItemArray[0].ToString());
        //            phongPublic.TenPhong = dt.Rows[i].ItemArray[1].ToString();
        //            phongPublic.TenLoaiPhong = dt.Rows[i].ItemArray[2].ToString();

        //            phongPublic.TinhTrang = dt.Rows[i].ItemArray[3].ToString();
        //            phongPublic.DonGia = int.Parse(dt.Rows[i].ItemArray[4].ToString());

        //            list.Add(phongPublic);
        //        }
        //        return list;
        //    }
        //    catch (Exception ex)
        //    {
        //        //bao loi
        //        MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
        //        return null;
        //    }
        //}

        public List<PhongPublic> selectPhongEmpty()
        {
            try
            {
                DataTable dt = new DataTable();
                List<PhongPublic> list = new List<PhongPublic>();


                int row;
                dt = connect.getData("Phong_SelectPhongEmpty");//,name,values,param);
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    PhongPublic phongPublic = new PhongPublic();
                    phongPublic.MaPhong = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    phongPublic.TenPhong = dt.Rows[i].ItemArray[1].ToString();
                    phongPublic.MaLoaiPhong = int.Parse(dt.Rows[i].ItemArray[2].ToString());
                    phongPublic.GhiChu = dt.Rows[i].ItemArray[3].ToString();
                    phongPublic.TinhTrang = dt.Rows[i].ItemArray[4].ToString();
                    list.Add(phongPublic);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }



        public void insert(PhongPublic phongPublic)
        {
            int param = 3;
            string[] name = new string[param];
            object[] values = new object[param];

            name[0] = "@TenPhong";
            name[1] = "@MaLoaiPhong";
            name[2] = "@GhiChu";

            values[0] = phongPublic.TenPhong;
            values[1] = phongPublic.MaLoaiPhong;
            values[2] = phongPublic.GhiChu;

            connect.ExcuteNonQuery("Phong_Insert", name, values, param);
        }

        public int update(PhongPublic phongPublic)
        {
            int param = 3;
            string[] name = new string[param];
            object[] values = new object[param];
            name[0] = "@TenPhong";
            //name[1] = "@TenPhong";
            name[1] = "@MaLoaiPhong";
            name[2] = "@GhiChu";
            //name[4] = "@TinhTrang";

            values[0] = phongPublic.TenPhong;
            //values[1] = phongPublic.TenPhong;
            values[1] = phongPublic.MaLoaiPhong;
            values[2] = phongPublic.GhiChu;
            //values[4] = phongPublic.TinhTrang;

            String procedureName = "Phong_Update";
            return connect.ExcuteNonQuery(procedureName, name, values, param);
        }

        public void deleteById(PhongPublic phongPublic)
        {
            String procedureName = "Phong_DeleteById";
            int param = 1;
            string[] name = new string[param];
            object[] values = new object[param];

            name[0] = "@MaPhong";

            values[0] = phongPublic.MaPhong;

            connect.ExcuteNonQuery(procedureName, name, values, param);
        }



        public void update_Day(PhongPublic phongPublic)
        {
            int param = 1;
            string[] name = new string[param];
            object[] values = new object[param];
            name[0] = "@TenPhong";
            values[0] = phongPublic.TenPhong;
            String procedureName = "Phong_Update_Day";

            connect.ExcuteNonQuery(procedureName, name, values, param);
        }

        public void update_Trong(PhongPublic phongPublic)
        {
            int param = 1;
            string[] name = new string[param];
            object[] values = new object[param];
            name[0] = "@TenPhong";
            values[0] = phongPublic.TenPhong;
            String procedureName = "Phong_Update_Trong";

            connect.ExcuteNonQuery(procedureName, name, values, param);
        }
    }
}